package br.com.iswe.gwt.ui.client.service;

import java.util.List;

import br.com.iswe.core.entidades.gwt.RemoteEntityObject;
import br.com.iswe.gwt.ui.client.ClientInfoContext;
import br.com.iswe.gwt.ui.client.CommonConstants;
import br.com.iswe.gwt.ui.client.RequestInfo;

import com.google.gwt.core.client.GWT;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;

@SuppressWarnings("unchecked")
public class MethodServiceClientImpl<R extends RemoteEntityObject, P extends RemoteEntityObject> extends ServiceClientAbstract {
	
	protected static final CommonConstants commonConstants = (CommonConstants) GWT.create(CommonConstants.class);
	
	/**
	 * ManterServiceAsync : Servico Manter
	 */
	protected MethodServiceAsync<R, P> serviceAsync;
	
	protected String methodName;
	
	public MethodServiceClientImpl(String controllerName, String methodName) {
		super(controllerName);
		this.methodName = methodName;
		this.serviceAsync = (MethodServiceAsync)GWT.create(MethodService.class); 
		((ServiceDefTarget)serviceAsync).setServiceEntryPoint(GWT.getModuleBaseURL() + "methodServiceImpl");
	}
	
	public MethodServiceAsync getServiceAsync() {
		return serviceAsync;
	}

	public void execute(P parameter, AsyncCallback<R> callback){
		execute(parameter, callback, null);
	}
	public void execute(P parameter, AsyncCallback<R> callback, RequestInfo requestInfo){
		if(requestInfo == null){
			requestInfo = new RequestInfo();
		}
		requestInfo.setup(ClientInfoContext.getClientInfoContext().getSessionInfo());
		
		serviceAsync.executeMethod(this.controllerName, methodName,
				parameter,
				requestInfo, 
				getCallBack(callback));
	}
	
	public void execute(List<P> parameterList, AsyncCallback<R> callback){
		execute(parameterList, callback, null);
	}
	public void execute(List<P> parameterList, AsyncCallback<R> callback, RequestInfo requestInfo){
		if(requestInfo == null){
			requestInfo = new RequestInfo();
		}
		requestInfo.setup(ClientInfoContext.getClientInfoContext().getSessionInfo());
		
		serviceAsync.executeMethod(this.controllerName, methodName,
				parameterList,
				requestInfo, 
				getCallBack(callback));
	}

	public void executeAsList(P parameter, AsyncCallback<List<R>> asyncCallback) {
		executeAsList(parameter, asyncCallback, null);
	}
	public void executeAsList(P parameter, AsyncCallback<List<R>> asyncCallback, RequestInfo requestInfo) {
		if(requestInfo == null){
			requestInfo = new RequestInfo();
		}
		requestInfo.setup(ClientInfoContext.getClientInfoContext().getSessionInfo());
		
		serviceAsync.executeMethodAsList(this.controllerName, methodName,
				parameter,
				requestInfo, 
				getCallBack(asyncCallback));
	}
	
	public void executeAsList(List<P> parameter, AsyncCallback<List<R>> asyncCallback) {
		executeAsList(parameter, asyncCallback, null);
	}
	public void executeAsList(List<P> parameter, AsyncCallback<List<R>> asyncCallback, RequestInfo requestInfo) {
		if(requestInfo == null){
			requestInfo = new RequestInfo();
		}
		requestInfo.setup(ClientInfoContext.getClientInfoContext().getSessionInfo());
		
		serviceAsync.executeMethodAsList(this.controllerName, methodName,
				parameter,
				requestInfo, 
				getCallBack(asyncCallback));
	}
	
}
